﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using PersonalHealth.Data.Model;
using Telerik.Windows.Controls;
using HealthManagement.Models;
using UI.Comm.Controls;
using UI.Comm.Common;

namespace HealthManagement.Views
{
    /// <summary>
    /// DoctorPanel.xaml 的交互逻辑
    /// </summary>
    public partial class DoctorPanel : Page
    {
        IList<Doctor> list = new List<Doctor>();
        public DoctorPanel()
        {
            InitializeComponent();

            init();
            if (UserGrid.Items.Count > 0)
                UserGrid.SelectedIndex = 0;
        }


        void init()
        {
            int records;
            int pages;
            IDoctorService userSer = new DoctorService();
            var orderby = new Func<Doctor, string>[] { new Func<Doctor, string>(c => c.UserName) };
            list = userSer.GetDoctorDetailList(c => c.UserTypeId != (int)UserType.Admin, orderby,
                        new bool[] { false }, 1, 20, out pages, out records);
            if (list == null)
                return;
            UserGrid.ItemsSource = list.Select(c => new UserModel { Id = c.Id, UserName = c.UserName, ServiceNo = c.ServiceNo });

        }


        private Doctor selectedUser = null;
        private void UserGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (UserGrid.SelectedItem == null)
                return;
            Doctor user = list.Single(c => c.Id == (UserGrid.SelectedItem as UserModel).Id);
            UserInfo.DataContext = selectedUser = user;
            tbPsd.Password = tbPsdConfirm.Password = user.Password;
        }

        private void rDataPager1_PageIndexChanged(object sender, Telerik.Windows.Controls.PageIndexChangedEventArgs e)
        {

        }

        private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string key = (sender as RadWatermarkTextBox).Text.Trim().ToLower();
            IList<Doctor> subList;
            if (key.Length > 0)
            {
                subList = list.Where(c => c.UserName.ToLower().Contains(key)).ToList();
              
            }
            else
            {
                subList = list.Where(c => 1 == 1).ToList();
            }
            if (subList == null)
                return;
            UserGrid.ItemsSource = subList.Select(c => new UserModel { Id = c.Id, UserName = c.UserName, ServiceNo = c.ServiceNo });
            if (UserGrid.Items.Count > 0)
                UserGrid.SelectedIndex = 0;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {

            PMWindow window = new DoctorForm(new Action<int>(
                (id) => { init(); }));
            window.Title = "新建医生";
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.ShowDialog();
        }

        private void del_Click(object sender, RoutedEventArgs e)
        {
            List<UserModel> clickList = Tools.CheckSelectItems<UserModel>(UserGrid);
            UserService ser = new UserService();

            if (clickList != null)
            {
                Tools.OpenConfirm("确认要删除选中的数据？", new EventHandler<WindowClosedEventArgs>((object o, WindowClosedEventArgs ex) =>
                {
                    if (ex.DialogResult != null && ex.DialogResult.Value)
                    {
                        foreach (UserModel item in clickList)
                        {
                            ser.Delete(new Doctor { Id = item.Id });
                        }
                    }
                }));
            }
            init();
            if (UserGrid.Items.Count > 0)
                UserGrid.SelectedIndex = 0;
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            int selectedIdx = UserGrid.SelectedIndex;
            IUserService ser = new UserService();
            Doctor doctor = UserInfo.DataContext as Doctor;
            if (string.IsNullOrEmpty(tbPsd.Password))
            {
                tbPsd.Password = "";
            }
            if (string.IsNullOrEmpty(tbPsdConfirm.Password))
            {
                tbPsdConfirm.Password = "";
            }
            if (tbPsd.Password != tbPsdConfirm.Password)
            {
                MessageBox.Show("密码输入不一致请重新输入！");
                tbPsd.Clear();
                tbPsdConfirm.Clear();
                tbPsd.Focus();
                return;
            }
            doctor.Password = tbPsd.Password;
            doctor.ModifyDate = DateTime.Now;
            ser.Update(doctor);

            init();
            if (UserGrid.Items.Count > 0)
                UserGrid.SelectedIndex = selectedIdx;

        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void RefreshClick(object sender, RoutedEventArgs e)
        {
            init();
            if (UserGrid.Items.Count > 0)
                UserGrid.SelectedIndex = 0;
        }
    }
}
